package com.situ.mall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.product.Carousel;
import com.situ.mall.pojo.entity.product.NewProduct;
import com.situ.mall.pojo.entity.product.Product;
import com.situ.mall.product.pojo.query.CarouselQuery;
import com.situ.mall.product.pojo.query.NewProductQuery;
import com.situ.mall.product.service.ICarouselService;
import com.situ.mall.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/product/carousel")
public class CarouselController {

	@Autowired
	private ICarouselService carouselService;
	
	@Autowired
	private IProductService productService;
	
	//【后台管理】——分页条件查询
	@GetMapping("/list")
	public Result<Map<String,Object>> list(CarouselQuery carouselQuery) {
		IPage<Carousel> page = new Page<>(carouselQuery.getPage(), carouselQuery.getLimit());
		QueryWrapper<Carousel> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(!ObjectUtils.isEmpty(carouselQuery.getName()), "name", carouselQuery.getName());
		queryWrapper.between(!ObjectUtils.isEmpty(carouselQuery.getBeginUpdateTime())&&!ObjectUtils.isEmpty(carouselQuery.getEndUpdateTime()),
				"update_time",carouselQuery.getBeginUpdateTime(),carouselQuery.getEndUpdateTime());
		queryWrapper.orderByAsc("priority");
		carouselService.page(page, queryWrapper);
		Map<String, Object> map = new HashMap<>();
		map.put("total", page.getTotal());
		map.put("records", page.getRecords());
		return Result.ok(map);
	}
	
	//【后台管理】——批量删除
	@DeleteMapping("/deleteAll/{ids}")
	public Result<Object> deleteAll(@PathVariable Integer[] ids) {
		carouselService.removeByIds(Arrays.asList(ids));
		return Result.ok("删除成功！");
	}
	
	//【后台管理】——单个删除
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id) {
		carouselService.removeById(id);
		return Result.ok("删除成功！");
	}
	
	//【后台管理】——添加
	@PostMapping("/add")
	public Result add(@RequestBody Carousel carousel) {
		if(carousel.getName()!=null&&carousel.getName()!=""){
			QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name", carousel.getName());
			Product product=productService.getOne(queryWrapper);
			if(product!=null){
				carousel.setProductId(product.getId());
			}else {
				return Result.error("产品不存在无法添加！");
			}
		}
		carouselService.save(carousel);
		return Result.ok("添加成功！");
	}
	
	//【后台管理】——单个查询
	@GetMapping("/selectById/{id}")
	public Result<Carousel> selectById(@PathVariable Long id) {
		Carousel carousel=carouselService.getById(id);
		return Result.ok(carousel);
	}
	
	//【后台管理】——修改
	@PutMapping("/update")
	public Result update(@RequestBody Carousel carousel) {
		if(carousel.getName()!=null&&carousel.getName()!=""){
			QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name", carousel.getName());
			Product product=productService.getOne(queryWrapper);
			if(product!=null){
				carousel.setProductId(product.getId());
			}else {
				return Result.error("产品不存在无法修改！");
			}
		}
		carouselService.saveOrUpdate(carousel);
		return Result.ok("修改成功！");
	}
	
	//【后台管理】——更新状态
	@PutMapping("/updateStatus/{id}/{status}")
	public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
		UpdateWrapper<Carousel> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id",id).set("status",status);
		carouselService.update(updateWrapper);
		return Result.ok("修改成功！");
	}
	
	//按照priority优先级查出前4张图片
	@GetMapping("/listPortal")
	public Result<List<Carousel>> listPortal() {
		List<Carousel> list = carouselService.listPortal();
		return Result.ok(list);
	}
}

